package com.example.mymusicdisplay;

import android.Manifest;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.example.mymusicdisplay.data.MusicData;
import com.example.mymusicdisplay.databinding.ActivityMainBinding;
import com.example.mymusicdisplay.viewmodel.MusicViewModel;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private ActivityMainBinding mMainBinding;
    private MusicViewModel mMusicViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        adjustDock();
//        if (ContextCompat.checkSelfPermission(this,
//                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager
//                .PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
//                Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
//                Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//            ActivityCompat.requestPermissions(this,
//                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
//                            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.INTERNET}, 1);
//        } else {
//            //TODO
//        }
        mMusicViewModel = new ViewModelProvider(
            this, new ViewModelProvider.NewInstanceFactory()).get(MusicViewModel.class);
        mMainBinding.setLifecycleOwner(this);
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.INTERNET, Manifest.permission.READ_EXTERNAL_STORAGE)
            .subscribe(new Consumer<Permission>() {
                @Override
                public void accept(Permission permission) throws Exception {
                    if (permission.granted) {
                        Log.d(TAG, "accept: true");
                        initListener();
                    } else if (permission.shouldShowRequestPermissionRationale) {
                        finish();
                    } else {
                        finish();
                    }
                }
            });
    }

    private void initListener() {
        MusicManager.getInstance().registerListener(new MusicManager.MusicChangedListener() {
            @Override
            public void musicStatusChanged(boolean status) {
                mMusicViewModel.getPlayStatus().postValue(status);
            }

            @Override
            public void musicPositionChanged(MusicData musicData) {
                mMusicViewModel.getCurrentMusic().postValue(musicData);
            }

            @Override
            public void musicProgressChanged(int progress) {
                mMusicViewModel.getProgress().postValue(progress);
            }
        });
    }


    //    @Override
//    public void onRequestPermissionsResult(int requestCode,
//                                           String[] permissions, int[] grantResults) {
//        switch (requestCode) {
//            case 1:
//                if (grantResults.length > 0 && grantResults[0]
//                        == PackageManager.PERMISSION_GRANTED) {
//                    //TODO
//                } else {
//                    Toast.makeText(this, getResources().getString(R.string.info),
//                            Toast.LENGTH_SHORT).show();
//                    finish();
//                }
//                break;
//            default:
//                break;
//        }
//    }

    private void adjustDock() {
        Window window = this.getWindow();
        View decorView = window.getDecorView();
        int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        decorView.setSystemUiVisibility(option);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.TRANSPARENT);
        window.setNavigationBarColor(Color.TRANSPARENT);
    }
}